package openai

import (
	"encoding/json"

	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/openai/aijieyun/pkg/services/aecs"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
	"github.com/gin-gonic/gin"
)

func DescribeInstances(c *gin.Context) {
	appG := app.Gin{C: c}

	client := aecs.DefaultClient

	request := ecs.CreateDescribeInstancesRequest()
	//todo query for different user
	//从数据库查询所有的机器的列表，然后通过描述去查询
	//重新去构建request

	response, err := client.DescribeInstances(request)
	if err != nil {
		// c.JSON(400, gin.H{"response": "", "err": err.Error()})
		appG.ResponseError(-1, err.Error())

		return
	}

	// 将响应转换为所需的格式
	var responseData map[string]interface{}
	jsonStr := response.GetHttpContentString()
	err = json.Unmarshal([]byte(jsonStr), &responseData)
	if err != nil {
		appG.ResponseError(-1, err.Error())
		return
	}

	appG.ResponseSuccess(gin.H{"response": responseData})
}
